Public Class CProfitabilityReport

    Private _customerID As Integer
    Private _supID As Integer
    Private _output As Report.EReportOutputType
    Private _bodyHTML As String
    Private _bodyHTMLForCreditNotes As String
    Private _bodyHTMLForCreditNotesExcel As String
    Private _costOSTotal As Decimal = 0
    Private _invoiceValueOSTotal As Decimal = 0
    Private _marginOSTotal As Decimal = 0
    Private _marginPercentOSTotal As Decimal = 0
    Private _valueCNTotal As Decimal = 0
    Private _invoiceValueCNTotal As Decimal = 0
    Private _marginCNTotal As Decimal = 0
    Private _marginPercentCNTotal As Decimal = 0


#Region "    Properties"

    Public ReadOnly Property GrandTotalCost() As Decimal
        Get
            Return _costOSTotal
        End Get
    End Property

    Public ReadOnly Property GrandTotalValue() As Decimal
        Get
            Return _invoiceValueOSTotal - _valueCNTotal
        End Get
    End Property

    Public ReadOnly Property GrandTotalMargin() As Decimal
        Get
            Return Me.GrandTotalValue - Me.GrandTotalCost
        End Get
    End Property

    Public ReadOnly Property GrandTotalMarginPercent() As Decimal
        Get
            Return Math.Round(Me.GrandTotalMargin / Me.GrandTotalCost * 100, 2)
        End Get
    End Property

    Public Property CustomerID() As Integer
        Get
            Return _customerID
        End Get
        Set(ByVal Value As Integer)
            _customerID = Value
        End Set
    End Property

    Public Property SupplierID() As Integer
        Get
            Return _supID
        End Get
        Set(ByVal Value As Integer)
            _supID = Value
        End Set
    End Property

    Private ReadOnly Property BodyHTML() As String
        Get
            If IsNothing(_bodyHTML) OrElse _bodyHTML.Trim = "" Then
                _bodyHTML = Me.GetTBodyHTML
            End If
            Return _bodyHTML
        End Get
    End Property

    Private ReadOnly Property BodyHTMLForCN() As String
        Get
            If IsNothing(_bodyHTMLForCreditNotes) OrElse _bodyHTMLForCreditNotes.Trim = "" Then
                _bodyHTMLForCreditNotes = Me.GetTBodyHTMLForCreditNotes
            End If
            Return _bodyHTMLForCreditNotes
        End Get
    End Property

    Private ReadOnly Property BodyHTMLForCNExcel() As String
        Get
            If IsNothing(_bodyHTMLForCreditNotesExcel) OrElse _bodyHTMLForCreditNotesExcel.Trim = "" Then
                _bodyHTMLForCreditNotesExcel = Me.GetTBodyHTMLForCreditNotesExcel
            End If
            Return _bodyHTMLForCreditNotesExcel
        End Get
    End Property


    ' 0: Screen (View)
    ' 1: Printer (Print)
    ' 2: File (Export)
    Public Property Output() As Report.EReportOutputType
        Get
            Return _output
        End Get
        Set(ByVal Value As Report.EReportOutputType)
            _output = Value
        End Set
    End Property
#End Region

    Protected Overridable Function GetReportHeaderHTML() As String
        Dim sb As New System.Text.StringBuilder
        sb.Append("<TABLE border=0 width=""100%"">")
        sb.Append("<TR>")
        sb.Append("<TD align=center colspan=8 class=txtnormal><b>PROFITABILITY REPORT</b></TD>")
        sb.Append("</TR>")

        sb.Append("<TR class=txtnormal>")
        sb.AppendFormat("<td align=left colspan=8>Customer: {0}</td>", CUser.GetName(Me.CustomerID))
        sb.Append("</TR>")
        sb.Append("<TR class=txtnormal>")
        sb.AppendFormat("<td align=left colspan=8>Supplier: {0}</td>", IIf(Me.SupplierID > 0, Common.GetLKDValue(Me.SupplierID, 1), "[All]"))
        sb.Append("</TR>")
        sb.Append("</TABLE >")
        Return sb.ToString
    End Function

    Public Sub RefreshReport()
        Me._bodyHTML = Me.GetTBodyHTML
        Me._bodyHTMLForCreditNotes = Me.GetTBodyHTMLForCreditNotes
    End Sub

    Public Function GetReportHTML() As String
        Dim sb As New System.Text.StringBuilder

        If Me.Output <> Report.EReportOutputType.Screen Then
            sb.Append(Me.GetReportHeaderHTML)
        End If

        ' report OrderStatus
        Select Case Me.Output
            Case Report.EReportOutputType.File
                sb.Append("<TABLE cellSpacing=1 cellPadding=3 width=""100%""  border=1>")
            Case Report.EReportOutputType.Printer
                sb.Append("<TABLE bordercolor=black cellSpacing=0 cellPadding=3 width=""100%""  border=1 style=""BORDER-COLLAPSE: collapse"">")
            Case Report.EReportOutputType.Screen
                sb.Append("<TABLE class=border cellSpacing=1 cellPadding=3 width=""100%"">")
        End Select
        sb.Append(Me.GetTHeadHTML)
        sb.Append(Me.BodyHTML)
        sb.Append("</TABLE>")

        Return sb.ToString
    End Function

    Public Function GetReportHTMLForCN() As String
        Dim sb As New System.Text.StringBuilder

        'report Credit notes
        Select Case Me.Output
            Case Report.EReportOutputType.File
                sb.Append("<TABLE cellSpacing=1 cellPadding=3 width=""100%""  border=1>")
                sb.Append(Me.GetTHeadHTMLForCNExcel)
                sb.Append(Me.BodyHTMLForCNExcel)
            Case Report.EReportOutputType.Printer
                sb.Append("<TABLE bordercolor=black cellSpacing=0 cellPadding=3 width=""100%""  border=1 style=""BORDER-COLLAPSE: collapse"">")
                sb.Append(Me.GetTHeadHTMLForCN)
                sb.Append(Me.BodyHTMLForCN)
            Case Report.EReportOutputType.Screen
                sb.Append("<TABLE width=""100%"" border=0 cellPadding=3 cellSpacing=1 class=""border"">")
                sb.Append(Me.GetTHeadHTMLForCN)
                sb.Append(Me.BodyHTMLForCN)
        End Select

        sb.Append("</TABLE>")

        Return sb.ToString
    End Function

    Protected Overridable Function GetTHeadHTML() As String
        Dim sb As New System.Text.StringBuilder

        sb.Append("<TR align=""center"">")
        sb.Append("<TD  valign=middle class=""txtheader""><strong>Order Number</strong></TD>")
        sb.Append("<TD  valign=middle class=""txtheader""><strong>Order Date</strong></TD>")
        sb.Append("<TD valign=middle class=""txtheader""><strong>Last updated<br>                            <em>(Week/Year)</em><strong></TD>")
        sb.Append("<TD  valign=middle class=""txtheader""><strong>Invoice No</strong></TD>")
        sb.Append("<TD width=100 valign=middle class=""txtheader""><strong>Cost<br>                          <em>                          (USD)</em><strong></TD>")
        sb.Append("<TD width=100 valign=middle class=""txtheader""><strong>Invoice Value<BR><i>(USD)</i></strong></TD>")
        sb.Append("<TD width=100 valign=middle class=""txtheader""><strong>Margin $<BR><i>(USD)</i></strong></TD>")
        sb.Append("<TD width=100 valign=middle class=""txtheader""><strong>Margin %</strong></TD>")
        sb.Append("</TR>")

        If Me.Output = Report.EReportOutputType.Screen Then
            sb.Insert(0, "<THEAD>")
            sb.Append("</THEAD>")
        End If

        Return sb.ToString
    End Function

    Protected Overridable Function GetTHeadHTMLForCN() As String
        Dim sb As New System.Text.StringBuilder

        sb.Append("<TR class='txtheader'>")
        sb.Append("<TD width='20%' height=25' align='center'><strong>Credit Date</strong></TD>")
        sb.Append("<TD width='20%' align='center'><strong>Credit Notes number</strong></TD>")
        sb.Append("<TD align='center'><strong>Description</strong></TD>")
        sb.Append("<TD width='100' align='center'><strong>Credit Note <em>(USD)</em></strong></TD>")
        sb.Append("</TR>")

        If Me.Output = Report.EReportOutputType.Screen Then
            sb.Insert(0, "<THEAD>")
            sb.Append("</THEAD>")
        End If

        Return sb.ToString
    End Function

    Protected Overridable Function GetTHeadHTMLForCNExcel() As String
        Dim sb As New System.Text.StringBuilder

        sb.Append("<TR class='txtheader'>")
        sb.Append("<TD width='20%' align='center'><strong>Credit Date</strong></TD>")
        sb.Append("<TD width='20%' align='center'><strong>Credit Notes number</strong></TD>")
        sb.Append("<TD align='center' colspan='3'><strong>Description</strong></TD>") ' 
        sb.Append("<TD width='100' align='center'><strong>Credit Note <em>(USD)</em></strong></TD>")
        sb.Append("</TR>")

        Return sb.ToString
    End Function

    Private Function GetMarginPercentText(ByVal invoiceValue As Decimal, ByVal cost As Decimal) As String
        Try
            Return FormatNumber(CDec(((invoiceValue - cost) / cost) * 100), 2)
        Catch ex As Exception
            Return "N/A"
        End Try
    End Function

    Private Function GetMarginPercent(ByVal invoiceValue As Decimal, ByVal cost As Decimal) As Decimal
        Try
            Return CDec(((invoiceValue - cost) / cost) * 100)
        Catch ex As Exception
            Return 0
        End Try
    End Function

    Protected Function GetMarginValueText(ByVal invoiceValue As Decimal, ByVal cost As Decimal) As String
        Return FormatNumber(CDec(invoiceValue - cost), 2)
    End Function

    Protected Function GetMarginValue(ByVal invoiceValue As Decimal, ByVal cost As Decimal) As Decimal
        Return CDec(invoiceValue - cost)
    End Function

    Private Function GetTRForOS(ByVal dr As DataRow) As String
        Dim i As Integer
        Dim sb As New System.Text.StringBuilder

        Dim cost As Decimal = Common.C2Dec(dr("COST"))
        Dim invoiceValue As Decimal = Common.C2Dec(dr("INVOICE_VALUE"))

        sb.Append("<tr align=center>")
        sb.AppendFormat("<td class='bgrow1'>{0}</td>", dr("ORDER_NUMBER"))
        sb.AppendFormat("<td class='bgrow1'>{0}</td>", Common.FormatDate(CDate(dr("ORDER_DATE"))))
        sb.AppendFormat("<td class='bgrow1'>{0}</td>", String.Format("{0}/{1}", Common.Week(CDate(dr("LAST_UPDATED"))), CDate(dr("LAST_UPDATED")).Year))
        sb.AppendFormat("<td class='bgrow1'>{0}</td>", dr("INVOICE_NO"))
        sb.AppendFormat("<td class='bgrow1' width='100' align=right>{0}</td>", FormatNumber(cost, 2))
        sb.AppendFormat("<td class='bgrow1' width='100' align=right>{0}</td>", FormatNumber(invoiceValue, 2))
        sb.AppendFormat("<td class='bgrow1' width='100' align=right>{0}</td>", Me.GetMarginValueText(invoiceValue, cost))
        sb.AppendFormat("<td class='bgrow1' width='100' align=right>{0}</td>", Me.GetMarginPercentText(invoiceValue, cost))
        sb.Append("</tr>")

        Me._costOSTotal += cost
        Me._invoiceValueOSTotal += invoiceValue
        'Me._marginPercentOSTotal += Me.GetMarginPercent(invoiceValue, cost)
        Me._marginOSTotal += Me.GetMarginValue(invoiceValue, cost)

        Return sb.ToString
    End Function

    Private Function GetTRForCN(ByVal dr As DataRow) As String
        Dim i As Integer
        Dim sb As New System.Text.StringBuilder

        Dim value As Decimal = Common.C2Dec(dr("VALUE_"))
        Dim invoiceValue As Decimal = Common.C2Dec(0)

        sb.Append("<TR>")
        sb.AppendFormat("<TD align='left' class='bgrow1'>{0}</TD>", Common.FormatDate(CDate(dr("CREATED_DATE"))))
        sb.AppendFormat("<TD align='left' class='bgrow1'>{0}</TD>", CStr(dr("NUMBER")))
        sb.AppendFormat("<TD align='left' class='bgrow1'>{0}</TD>", CStr(dr("DESCRIPTION")))
        sb.AppendFormat("<TD align='right' class='bgrow1'><span class=""hightlightred"">-{0}</span></TD>", FormatNumber(value, 2))
        sb.Append("</TR>")

        Me._valueCNTotal += value
        Me._invoiceValueCNTotal += invoiceValue
        'Me._marginPercentCNTotal += Me.GetMarginPercent(invoiceValue, value)
        Me._marginCNTotal += Me.GetMarginValue(invoiceValue, value)

        Return sb.ToString
    End Function

    Private Function GetTRForCNExcel(ByVal dr As DataRow) As String
        Dim i As Integer
        Dim sb As New System.Text.StringBuilder

        Dim value As Decimal = Common.C2Dec(dr("VALUE_"))
        Dim invoiceValue As Decimal = Common.C2Dec(0)

        sb.Append("<TR>")
        sb.AppendFormat("<TD align='left' class='bgrow1'>{0}</TD>", Common.FormatDate(CDate(dr("CREATED_DATE"))))
        sb.AppendFormat("<TD align='left' class='bgrow1'>=T(""{0}"")</TD>", CStr(dr("NUMBER")))
        sb.AppendFormat("<TD align='left' class='bgrow1' colspan='3'>=T(""{0}"")</TD>", CStr(dr("DESCRIPTION")))
        sb.AppendFormat("<TD align='right' class='bgrow1'><font color='red'>-{0}</font></TD>", FormatNumber(value, 2))
        sb.Append("</TR>")

        Me._valueCNTotal += value
        Me._invoiceValueCNTotal += invoiceValue
        'Me._marginPercentCNTotal += Me.GetMarginPercent(invoiceValue, value)
        Me._marginCNTotal += Me.GetMarginValue(invoiceValue, value)

        Return sb.ToString
    End Function

    Protected Overridable Function GetTBodyHTML() As String

        Dim objOS As New COrderStatus
        Dim ds As DataSet = objOS.GetLastestOrderStatusList(Me.CustomerID, Me.SupplierID)

        _costOSTotal = 0
        _invoiceValueOSTotal = 0
        _marginOSTotal = 0
        _marginPercentOSTotal = 0


        Dim sb As New System.Text.StringBuilder

        Dim dr As DataRow
        For Each dr In ds.Tables(0).Rows
            sb.Append(Me.GetTRForOS(dr))
        Next

        ' Add Total
        sb.Append("<TR align=center class=bggrey>")
        sb.Append("<TD height=19 class=txtcolor1><strong>TOTAL</strong></TD>")
        sb.Append("<TD>&nbsp;</TD>")
        sb.Append("<TD>&nbsp;</TD>")
        sb.Append("<TD>&nbsp;</TD>")
        sb.AppendFormat("<TD width='100' class=txtcolor1 align=right><strong>{0}</strong></TD>", FormatNumber(Me._costOSTotal, 2))
        sb.AppendFormat("<TD width='100' class=txtcolor1 align=right><strong>{0}</strong></TD>", FormatNumber(Me._invoiceValueOSTotal, 2))
        sb.AppendFormat("<TD width='100' class=txtcolor1 align=right><strong>{0}</strong></TD>", FormatNumber(Me._marginOSTotal, 2))
        sb.AppendFormat("<TD width='100' class=txtcolor1 align=right><strong>{0}</strong></TD>", Me.GetMarginPercentText(Me._invoiceValueOSTotal, Me._costOSTotal))
        sb.Append("</TR>")

        If Me.Output = Report.EReportOutputType.Screen Then
            sb.Insert(0, "<TBODY>")
            sb.Append("</TBODY>")
        End If

        Return sb.ToString
    End Function

    Protected Overridable Function GetTBodyHTMLForCreditNotes() As String

        Dim objCN As New CreditNotes
        Dim ds As DataSet = objCN.GetCreditNotesListBySupplier(Me.CustomerID, Me.SupplierID)

        Dim sb As New System.Text.StringBuilder

        _valueCNTotal = 0
        _invoiceValueCNTotal = 0
        _marginCNTotal = 0
        _marginPercentCNTotal = 0

        Dim dr As DataRow
        For Each dr In ds.Tables(0).Rows
            sb.Append(Me.GetTRForCN(dr))
        Next

        ' Add Total
        sb.Append("<TR align='center' class='bggrey'>")
        sb.Append("<TD height='19' class='txtcolor1'><strong>TOTAL</strong></TD>")
        sb.Append("<TD>&nbsp;</TD>")
        sb.Append("<TD>&nbsp;</TD>")
        sb.AppendFormat("<TD class='txtcolor1' align=right><strong><span class=""hightlightred"">-{0}</span></strong></TD>", FormatNumber(Me._valueCNTotal, 2))
        sb.Append("</TR>")

        If Me.Output = Report.EReportOutputType.Screen Then
            sb.Insert(0, "<TBODY>")
            sb.Append("</TBODY>")
        End If

        Return sb.ToString
    End Function

    Protected Overridable Function GetTBodyHTMLForCreditNotesExcel() As String

        Dim objCN As New CreditNotes
        Dim ds As DataSet = objCN.GetCreditNotesListBySupplier(Me.CustomerID, Me.SupplierID)

        Dim sb As New System.Text.StringBuilder

        _valueCNTotal = 0
        _invoiceValueCNTotal = 0
        _marginCNTotal = 0
        _marginPercentCNTotal = 0

        Dim dr As DataRow
        For Each dr In ds.Tables(0).Rows
            sb.Append(Me.GetTRForCNExcel(dr))
        Next

        ' Add Total
        sb.Append("<TR align='center' class='bggrey'>")
        sb.Append("<TD class='txtcolor1'><strong>TOTAL</strong></TD>")
        sb.Append("<TD>&nbsp;</TD>")
        sb.Append("<TD colspan='3'>&nbsp;</TD>")
        sb.AppendFormat("<TD class='txtcolor1' align=right><strong><font color='red'>-{0}</font></strong></TD>", FormatNumber(Me._valueCNTotal, 2))
        sb.Append("</TR>")

        If Me.Output = Report.EReportOutputType.Screen Then
            sb.Insert(0, "<TBODY>")
            sb.Append("</TBODY>")
        End If

        Return sb.ToString
    End Function

    Public Sub New(ByVal customerID As Integer, ByVal supplierID As Integer, ByVal out As Report.EReportOutputType)
        Me._customerID = customerID
        Me._output = out
        Me._supID = supplierID
    End Sub

    Public Sub New()

    End Sub

End Class
